--[[ $Id: Core.lua 57673 2007-12-30 14:59:44Z darkimakuni $ ]]

TeamSqueak = Rock:NewAddon("TeamSqueak", 'LibRockComm-1.0', 'LibRockConsole-1.0', 'LibRockConfig-1.0', 'LibRockDB-1.0', 'LibRockEvent-1.0')
TeamSqueak.date = ("$Date: 2007-12-30 14:59:44 +0000 (Sun, 30 Dec 2007) $"):match("%d%d%d%d%-%d%d%-%d%d")
TeamSqueak.revision = ("$Revision: 57673 $"):match("%d+")

TeamSqueak:SetDatabase("TeamSqueakDB")
TeamSqueak:SetDatabaseDefaults('profile', {
	list = { detached = true },
	groupLeader  = { r = 1, g = 1, b = 1 },
	groupOfficer = { r = 0.85, g = 0.85, b = 0.85 },
	groupMember  = { r = 0.7, g = 0.7, b = 0.7 },
})


local L = Rock("LibRockLocale-1.0"):GetTranslationNamespace("TeamSqueak")
local Tablet = Rock("Tablet-2.0")

BINDING_HEADER_TEAMSQUEAK = "TeamSqueak"
BINDING_NAME_TEAMSQUEAK = L["Push To Talk"]


function TeamSqueak:OnEnable()
	self.registry = {}

	self:AddEventListener("CHAT_MSG_SYSTEM")

	self:SetCommPrefix("TSK")
	self:AddCommListener("TSK", "GROUP")

	self:AddSlashCommand("OnSlashCommand", { "/ts", "/TeamSqueak" })

	Tablet:Register("TeamSqueak_List",
		'detachedData', self.db.profile.list, 'dontHook', true, 'hideWhenEmpty', true,
		'minWidth', 350, 'strata', "HIGH", 'cantAttach', true, 'children', function()
			local cat = Tablet:AddCategory('columns', 1, 'text', "TeamSqueak", 'textR', 1, 'textG', 0.7, 'textB', 0)
			cat:AddLine()

			for _, v in pairs(self.registry) do
----				if type(v) == 'table' then
					local r, g, b

					if v.rank == 2 then -- Raid/Party Leader
						r, g, b = self.db.profile.groupLeader.r, self.db.profile.groupLeader.g, self.db.profile.groupLeader.b
					elseif v.rank == 1 then -- Raid Officer
						r, g, b = self.db.profile.groupOfficer.r, self.db.profile.groupOfficer.g, self.db.profile.groupOfficer.b
					else -- Raid/Party Member
						r, g, b = self.db.profile.groupMember.r, self.db.profile.groupMember.g, self.db.profile.groupMember.b
					end

					cat:AddLine('text', v.name, 'size', 10, 'textR', r, 'textG', b, 'textB', b)
----				end
			end
		end
	)

	if not GetBindingKey("TEAMSQUEAK") then
		self:Print(L["Welcome to TeamSqueak! Please use '/ts config' to open the options window, and set your Push To Talk button."])
	end
end

function TeamSqueak:OnDisable()
	self.registry = nil
	Tablet:Unregister("TeamSqueak_List")
end

function TeamSqueak:OnCommReceive(prefix, sender, distribution, chatting, rank)
	self:SetTalking(sender, chatting, rank)
end

function TeamSqueak:OnSlashCommand(cmd, txt)
	if txt == "config" or txt == "options" then
		self:OpenConfigMenu()
	elseif txt == "close" or txt == "hide" then
		Tablet:Close("TeamSqueak_List")
	else
		Tablet:Open("TeamSqueak_List")
	end
end


function TeamSqueak:OnButtonDown()
	local rank = 0

	if IsRaidLeader() or IsPartyLeader() then rank = 2
	elseif IsRaidOfficer() then rank = 1 end

	self:SendCommMessage("GROUP", true, rank)
end

function TeamSqueak:OnButtonUp()
	self:SendCommMessage("GROUP", false)
end


function TeamSqueak:SetTalking(name, chatting, rank)
	local id

	for k, v in pairs(self.registry) do
		if v and v.name == name then
			id = k
		end
	end

	if chatting and not id then
		table.insert(self.registry, { name = name, rank = rank })
	elseif not chatting and id then
		table.remove(self.registry, id)
	end

	Tablet:Refresh("TeamSqueak_List")
end


-- we cannot receive comm messages when someone/you leaves the group, remove names as necessary
function TeamSqueak:CHAT_MSG_SYSTEM(msg)
	if msg:find(L["(%s) leaves the party."]) then
		self:SetTalking(msg:match(L["(%s) leaves the party."]), false)
	end

	if msg:find(L["(%s) has left the raid group"]) then
		self:SetTalking(msg:match(L["(%s) has left the raid group"]), false)
	end

	if msg == ERR_LEFT_GROUP_YOU or msg == ERR_RAID_YOU_LEFT or msg == ERR_GROUP_DISBANDED then
		self:Purge()
	end
end

function TeamSqueak:Purge()
	self.registry = {}
	Tablet:Refresh("TeamSqueak_List")
end
